package com.ts.api.module.api.convert.receive.adapter;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.common.redis.ApiMatchRedisKey;
import com.ts.api.module.api.convert.receive.entity.ApiReceiveNotice;
import com.ts.api.module.api.convert.receive.handler.ApiReceiveNoticeHandler;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *  上游-对方接入我方-拉取进件通知
 *
 *  @author Pan
 */
@Service("apiReceivePullApplyNoticeAdapter")
public class ApiReceivePullApplyNoticeAdapter implements ApiReceiveNoticeHandler {

    @Override
    public Map<String, Object> buildRequest(ApiReceiveNotice apiReceiveNotice) throws Exception {
        String phoneMd5 = apiReceiveNotice.getPhoneMd5();
        //  获取撞库时保存的applyId
        String key = ApiMatchRedisKey.getKey(apiReceiveNotice.getReqId(), phoneMd5);
        String applyId = ApiMatchRedisKey.API_CHANNEL_DATA_EXCHANGE_REQ_ID.get(key, String.class);
        return MapUtils.newHashMap(newMap -> newMap.put("applyId", applyId));
    }

    @Override
    public boolean hasSuc(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return false;
        }
        return Eq.object(jsonObject.getInteger("code"), 0);
    }
}
